import axios from 'axios';
import qs from 'querystring';
import _ from 'lodash';
import { downloadFile } from '@util/download-file';
import { DEVICELIST } from '@util/trans-device-name';

export const addOneday = body => {
  return axios.post('/api/task/oneday-task', body);
};

export const batchAddOneday = body => {
  return axios.post('/api/task/batchAddOneday', body);
};

export const customAddOneday = body => {
  return axios.post('/api/task/customAddOneday', body);
};

export const addZoomeye = body => {
  return axios.post('/api/task/addZoomeye', body);
};

export const addWeb = body => {
  return axios.post('/api/task/addWeb', body);
};

export const getTasksList = params => {
  return axios.get(`/api/task/tasks?${qs.stringify(params)}`);
};

export const batchDeleteTasks = body => {
  return axios.post('/api/task/batchDeleteTask', body);
};

export const getZoomeyeIps = params => {
  if (params.device) {
    let device = _.invert(DEVICELIST)[params.device];
    if (device) {
      params.device = device;
    }
  }
  return axios.get(`/api/task/detectionIpList?${qs.stringify(params)}`);
};

export const getSurvivalPorts = params => {
  return axios.get(`/api/task/detectionIpPortInfoList?${qs.stringify(params)}`);
};

//IP管理页面的增删查改
export const getTargetList = filter => {
  const formatFiler = _.pickBy(filter, value => Boolean(value));
  return axios.post('/api/task/targetList', formatFiler);
};

export const addTarget = body => {
  return axios.post('/api/task/addTarget', body);
};

export const updateTarget = body => {
  return axios.post('/api/task/updateTarget', body);
};

export const deleteTarget = body => {
  return axios.post('/api/task/deleteTarget', body);
};

//端口管理页面的增删查改
export const getPortList = filter => {
  const formatedFiler = _.pickBy(filter, value => Boolean(value));
  const query = qs.stringify(formatedFiler);
  return axios.get(`/api/task/portList?${query}`);
};

export const addPort = body => {
  return axios.post('/api/task/addPort', body);
};

export const modPort = body => {
  return axios.post('/api/task/modPort', body);
};

export const deletePort = body => {
  return axios.post('/api/task/deletePort', body);
};

//IP白名单管理页面的增删查改
export const getWhiteList = filter => {
  const formatedFiler = _.pickBy(filter, value => Boolean(value));
  const query = qs.stringify(formatedFiler);
  return axios.get(`/api/task/whiteList?${query}`);
};

export const addWhiteIp = body => {
  return axios.post('/api/task/addWhiteIp', body);
};

export const modWhiteIp = body => {
  return axios.post('/api/task/modWhiteIp', body);
};

export const deleteWhiteIp = body => {
  return axios.post('/api/task/deleteWhiteIp', body);
};

export const dropWhiteIp = () => {
  return axios.post('/api/task/dropWhiteIp');
};

export const batchDelWhiteIp = body => {
  return axios.post('/api/task/batchDelWhiteIp', body);
};

//脆弱性管理页面的增删查改
export const getVulnerabilityList = filter => {
  const formatedFiler = _.pickBy(filter, value => Boolean(value));
  const query = qs.stringify(formatedFiler);
  return axios.get(`/api/task/vulnerabilityList?${query}`);
};

export const addVulnerability = body => {
  return axios.post('/api/task/addVulnerability', body);
};

export const modVulnerability = body => {
  return axios.post('/api/task/modVulnerability', body);
};

export const deleteVulnerability = body => {
  return axios.post('/api/task/deleteVulnerability', body);
};

export const getAllPortTemplates = () => {
  return axios.get('/api/task/listAllPorts');
};

export const addOnedayStrategy = body => {
  return axios.post('/api/task/addOnedayStrategy', body);
};

export const updateOnedayStrategy = body => {
  return axios.post('/api/task/modOnedayStrategy', body);
};

export const deleteOnedayStrategy = id => {
  return axios.post('/api/task/deleteOnedayStrategy', {
    // eslint-disable-next-line
    strategy_id: id
  });
};

export const getOnedayStrategtList = params => {
  return axios.get(`/api/task/onedayStrategyList?${qs.stringify(params)}`);
};

export const getOneDayList = filter => {
  const formattedFiler = _.pickBy(filter, value => Boolean(value));
  const query = qs.stringify(formattedFiler);
  return axios.get(`/api/task/onedayDetailList?${query}`);
};

export const deleteOneDayDetail = _id => {
  return axios.delete(`/api/task/onedayDetail/${_id}`);
};

export const getAllHasPocOnedayList = () => {
  return axios.get('/api/task/listAllDetails');
};

// 复测任务
export const restartTask = id => {
  return axios.post('/api/task/restartTask', {
    // eslint-disable-next-line
    task_id: id
  });
};

// 暂停任务
export const stopTask = id => {
  return axios.post('/api/task/stopTask', {
    // eslint-disable-next-line
    task_id: id
  });
};

// 取消任务｜终止任务
export const cancelTask = id => {
  return axios.post('/api/task/cancelTask', {
    // eslint-disable-next-line
    task_id: id
  });
};

// 恢复｜启动任务
export const resumeTask = id => {
  return axios.post('/api/task/resumeTask', {
    // eslint-disable-next-line
    task_id: id
  });
};

export const updateTaskCycle = body => {
  return axios.post('/api/task/updateCycle', body);
};

export const getOnedayDetail = id => {
  return axios.get(`/api/task/getOnedayDetail?oneday_detail_id=${id}`);
};

export const getOnedayTaskDetail = id => {
  return axios.get(`/api/task/oneday-task?taskId=${id}`);
};

export const getOnedayTaskIpList = params => {
  return axios.get(`/api/task/onedayTaskVulList?${qs.stringify(params)}`);
};

export const getZoomeyeExportList = taskId => {
  return axios.get(`/api/task/detection/xlsx?taskId=${taskId}`);
};

export const downloadZoomeyeExport = params => {
  return downloadFile('get', `/api/task/detection/xlsx/download?${qs.stringify(params)}`);
};

export const deleteZoomeyeExport = body => {
  return axios.post('/api/task/detection/xlsx/delete', body);
};

export const exportZoomeyeExport = body => {
  return axios.post('/api/task/detection/xlsx', body);
};

//oneday任务导出报告相关, 获取报告列表、下载报告、删除报告、导出报告
export const getOnedayExportList = taskId => {
  return axios.get(`/api/task/showOnedayTask?task_id=${taskId}`);
};

export const downloadOnedayExport = params => {
  return downloadFile('get', `/api/task/downloadOnedayTask?${qs.stringify(params)}`);
};

export const deleteOnedayExport = body => {
  return axios.post('/api/task/deleteOnedayTask', body);
};

export const exportOnedayExport = filter => {
  const formatedFiler = _.pickBy(filter, value => Boolean(value));
  const query = qs.stringify(formatedFiler);
  return axios.get(`/api/task/exportOnedayTask?${query}`);
};

export const getStatics = () => {
  return axios.get('/api/static/getStatics');
};

export const getTop10Fingers = type => {
  return axios.get(`/api/static/getTopNFingers?stepKey=${type}&limit=10`);
};

export const getTop10Vuls = level => {
  if (level === 'all') {
    return axios.get('/api/static/getTop10AffectedSites');
  }
  return axios.get(`/api/static/getTop10AffectedSites?level=${level}`);
};

export const getAllStrategies = () => {
  return axios.get('/api/task/listAllStrategies');
};

// oneday探测结果报告
export const getOnedayTaskReportList = taskId => {
  return axios.get(`/api/task/showOnedayTaskReport?task_id=${taskId}`);
};

export const downloadOnedayTaskReport = params => {
  return downloadFile('get', `/api/task/downloadOnedayTaskReport?${qs.stringify(params)}`);
};

export const deleteOnedayTaskReport = body => {
  return axios.post('/api/task/deleteOnedayTaskReport', body);
};

export const exportOnedayTaskReport = filter => {
  const formatedFiler = _.pickBy(filter, value => Boolean(value));
  const query = qs.stringify(formatedFiler);
  return axios.get(`/api/task/exportOnedayTaskReport?${query}`);
};

// 资产探测探测报告
export const getZoomeyeTaskReportList = taskId => {
  return axios.get(`/api/task/detection/docx?taskId=${taskId}`);
};

export const downloadZoomeyeTaskReport = params => {
  return downloadFile('get', `/api/task/detection/docx/download?${qs.stringify(params)}`);
};

export const deleteZoomeyeTaskReport = body => {
  return axios.post('/api/task/detection/docx/delete', body);
};

export const exportZoomeyeTaskReport = body => {
  return axios.post('/api/task/detection/docx', body);
};

// 获取任务详情探测目标
export const getTaskTarget = params => {
  const formatedFiler = _.pickBy(params, value => Boolean(value));
  const query = qs.stringify(formatedFiler);
  return axios.get(`/api/task/getTaskTarget?${query}`);
};

// web指纹探测
export const getWebDetail = taskId => {
  return axios.get(`/api/task/getWebDetail?task_id=${taskId}`);
};

export const getWebResult = params => {
  return axios.get(`/api/task/getWebResult?${qs.stringify(params)}`);
};

//Web指纹探测报告 (word)
export const getWebTaskReportList = taskId => {
  return axios.get(`/api/task/detection/docx?taskId=${taskId}`);
};

export const downloadWebTaskReport = params => {
  return downloadFile('get', `/api/task/detection/docx/download?${qs.stringify(params)}`);
};

export const deleteWebTaskReport = body => {
  return axios.post('/api/task/detection/docx/delete', body);
};

export const exportWebTaskReport = body => {
  return axios.post('/api/task/detection/docx', body);
};
// Web指纹探测报告 (excel)
export const getWebExportList = taskId => {
  return axios.get(`/api/task/detection/xlsx?taskId=${taskId}`);
};

export const downloadWebExport = params => {
  return downloadFile('get', `/api/task/detection/xlsx/download?${qs.stringify(params)}`);
};

export const deleteWebExport = body => {
  return axios.post('/api/task/detection/xlsx/delete', body);
};

export const exportWebExport = body => {
  return axios.post('/api/task/detection/xlsx', body);
};

export const getRelatedOnedayList = params => {
  return axios.get(`/api/task/getRelatedOnedayList?${qs.stringify(params)}`);
};

export const getRunningTaskCount = () => {
  return axios.get('/api/task/runningTaskCount');
};

/**
 * 添加探测任务
 * @param {*} body
 */
export const addDetectionTask = body => {
  return axios.post('/api/task/detection', body);
};

/**
 * 添加漏洞普查任务
 * @param {*} body
 */
export const addVulDetectionTask = body => {
  return axios.post('/api/task/detection/vul', body);
};

/**
 * 获取漏洞普查任务详情
 * @param {*} params
 */
export const getVulDetectionTask = taskId => {
  return axios.get(`/api/task/detection/vul?taskId=${taskId}`);
};

/**
 * 添加资产探测任务
 * @param {string} taskId
 */
export const getDetectionDetail = taskId => {
  return axios.get(`/api/task/detection?taskId=${taskId}`);
};

/**
 * 添加资产探测任务
 * @param {string} realName
 * @param {string} fileName
 */
export const downloadDetectionAttachment = (realName, fileName) => {
  return downloadFile('get', `/api/task/attachment?fileName=${realName}`, null, { fileName });
};

/**
 * 编辑或添加poc
 */
export const addPOC = body => {
  return axios.post('/api/external/addPoc', body);
};

/**
 * 下载POC文件
 * @param filename
 */
export const downloadPoc = filename => {
  return downloadFile('get', `/api/external/poc/download?filename=${filename}`);
};

/**
 * 获取资产，漏洞普查配置
 * */
export const getDetectionCache = () => {
  return axios.get('/api/task/detection/config');
};

// 获取任务轮次信息
export const getTaskHistory = taskId => {
  return axios.get(`/api/task/history?taskId=${taskId}`);
};

// 导出漏洞普查任务DOCX报告
export const exportVulDetectReport = params => {
  return axios.get(`/api/task/detection/vul/docx?${qs.stringify(params)}`);
};

// 获取漏洞普查任务报告列表
export const showVulDetectReport = taskId => {
  return axios.get(`/api/task/detection/vul/docx/show?taskId=${taskId}`);
};

// 下载漏洞普查任务报告
export const downloadVulDetectTaskReport = params => {
  return downloadFile('get', `/api/task/detection/vul/docx/download?${qs.stringify(params)}`);
};

// 删除漏洞普查任务报告
export const deleteVulDetectTaskReport = body => {
  return axios.post('/api/task/detection/vul/docx/delete', body);
};

// 导出漏洞普查任务DOCX报告
export const exportVulDetectTaskXlsx = params => {
  return axios.get(`/api/task/detection/vul/xlsx?${qs.stringify(params)}`);
};

// 获取漏洞普查报表列表
export const showVulDetectTaskXlsx = taskId => {
  return axios.get(`/api/task/detection/vul/xlsx/show?taskId=${taskId}`);
};

// 下载漏洞普查报表
export const downloadVulDetectTaskXlsx = params => {
  return downloadFile('get', `/api/task/detection/vul/xlsx/download?${qs.stringify(params)}`);
};

// 删除漏洞普查报表
export const deleteVulDetectTaskXlsx = body => {
  return axios.post('/api/task/detection/vul/xlsx/delete', body);
};

// 管理员分页搜
export const getStaffList = params => {
  return axios.get(`/api/task/target/staffList?${qs.stringify(params)}`);
};

// 探测策略列表
export const getProbeStrategyList = params => {
  return axios.get(`/api/task/detectionStrategy/list?${qs.stringify(params)}`);
};

// 新增探测策略
export const addProbeStrategy = body => {
  return axios.post('/api/task/detectionStrategy', body);
};

// 修改探测策略
export const updateProbeStrategy = body => {
  return axios.post('/api/task/detectionStrategy/update', body);
};

// 删除探测策略
export const deleteProbeStrategy = body => {
  return axios.post('/api/task/detectionStrategy/del', body);
};

// 扫描速率列表
export const getscanRateList = params => {
  return axios.get(`/api/scanRate/list?${qs.stringify(params)}`);
};

// 新增扫描速率
export const addscanRate = body => {
  return axios.post('/api/scanRate/add', body);
};

// 修改扫描速率
export const editScanRate = body => {
  return axios.post('/api/scanRate/edit', body);
};

// 删除扫描速率
export const deleteScanRate = body => {
  return axios.delete('/api/scanRate/delete', { data: body });
};

// 获取漏洞插件列表接口
export const getDetailOnedayList = params => {
  return axios.get(`/api/task/detail/onedayList?${qs.stringify(params)}`);
};
